import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import javadsd from '@dfeidao/atom-web/msg/javadsd';
import nodejs from '@dfeidao/atom-web/msg/nodejs';

import tree from '@dfeidao/fd-w000001';
// import nodejs from '@dfeidao/atom-web/msg/nodejs';
// import { refsystemid } from './config';

// import whoami from '@dfeidao/atom-web/msg/whoami';
import render from '@dfeidao/atom-web/render/render';
import P05 from './P05';
interface B {
	docDataStore: {
		master: {
			dataArray: [{
				no: string;
				group_number: string
				group_name: string;
				formid: string;
				formname: string;
				grid_1: {
					dataArray: [
						{
							no: string;
							group_number: string;
							group_name: string;
							formid: string;
							formname: string;
						}
					]
				}
			}]
		};
	};
}
export default async function a008(fd: IFeidaoAiBrowserComponent) {
	// const userinf = await whoami();
	const productid1 = get(fd, 'productid');
	const select1 = fd.data.node.querySelector<HTMLSelectElement>('[select]');
	const index = select1.options.selectedIndex;
	const client_type1 = select1.options[index].value;
	const condition = {
		param: {
			productid: productid1,
			client_type: client_type1
		}
	};
	set(fd, 'client_type', client_type1);
	const ret = await javadsd('12', '12', 'smart_securitydsd0000010', condition) as B;
	const array = new Array();
	ret.docDataStore.master.dataArray.forEach((i) => {
		i.group_number = i.group_number ? i.group_number : 'weifenzhu';
		if (i.grid_1.dataArray.length > 0) {
			i.grid_1.dataArray.forEach((j) => {
				j.no = i.group_number;
				array.push(j);
			});
		}
		i.formid = i.group_number;
		i.formname = i.group_name;
		delete i.grid_1;
		i.no = '';
		// if (!i.formid) {
		// 	i.formid = JSON.stringify(idex);
		// }
		array.push(i);
	});
	render(fd, JSON.stringify(array), P05, 'P05', 'inner');
	const tenantid = get(fd, 'tenantid') as string;
	if (tenantid) {
		const condition2 = {
			param: {
				productid: tenantid,
				client_type: client_type1
			}
		};
		const ret1 = await javadsd('12', '12', 'smart_securitydsd0000010', condition2) as B;
		const select_array = new Array();
		const array1 = new Array();

		ret1.docDataStore.master.dataArray.forEach((i) => {
			i.group_number = i.group_number ? i.group_number : 'weifenzhu';
			if (i.grid_1.dataArray.length > 0) {
				i.grid_1.dataArray.forEach((j) => {
					j.no = i.group_number;
					select_array.push(j.formid);
					array1.push(j);
				});
			}
			// i.formid = i.group_number;
			i.formname = i.group_name;
			delete i.grid_1;
			i.no = '';
			// if (!i.formid) {
			// 	i.formid = JSON.stringify(idex);
			// }
			// array1.push(i);
			select_array.push(i.formid);
		});

		const zds = fd.data.node.querySelector('[zds]') as tree;
		zds.uncheck_all();
		// console.log(array1);
		// set(fd, 'all_data', array1);

		zds.check_node(select_array);
		// debugger;
		const formid_arr = new Array();
		array1.forEach((v: { formid: string }) => {
			formid_arr.push(v.formid);
		});
		// console.log(formid_arr);
		const res1 = await nodejs<{ dataArray: Array<{ action_name: string, formid: string, action_keyword: string, code: string, _id: string, productid: string }> }>('tenant-function/zj-000001/s004', { filter: { productid: productid1, formid_arr } });//
		const a = new Map();
		// console.log(res1);
		res1.dataArray.forEach((i) => {
			if (Array.from(a.keys()).indexOf(i.formid) === -1) {
				a.set(i.formid, {
					left: {},
					right: [i]
				});
			} else {
				const te = a.get(i.formid);
				te.right.push(i);
			}
		});
		// console.log(array1);

		array1.forEach((item) => {
			if (item.group_name === "未分组") {
				item.group_name = '';
			}
			if (Array.from(a.keys()).indexOf(item.formid) === -1) {

				a.set(item.formid, {
					left: { client_type: item.client_type, create_time: item.create_time, formid: item.formid, formname: item.formname, group_number: item.group_number, group_name: item.group_name, package_no: item.package_no, package_name: item.package_name, package_path: item.package_path, productid: tenantid },
					right: []
				});
			} else {
				const te = a.get(item.formid);
				te.left = { client_type: item.client_type, create_time: item.create_time, formid: item.formid, formname: item.formname, group_number: item.group_number, group_name: item.group_name, package_no: item.package_no, package_name: item.package_name, package_path: item.package_path, productid: tenantid };
			}

		});
		// debugger;
		// console.log(a);
		set(fd, 'origin_obj', a);
	}
}
